﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.mainmenu {
    import flash.events.*;
    import org.robotlegs.core.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.components.mainmenu.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;

    public class ShopMenuMediator extends Mediator implements IMediator {

		[Inject]
        public var shopMenu:ShopMenu;
		[Inject]
        public var languageModel:LanguageModel;
		[Inject]
        public var gameStateModel:GameStateModel;
		[Inject]
        public var sunModel:SunModel;
		[Inject]
        public var tickModel:TickModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var tutorialModel:TutorialModel;
		[Inject]
        public var roomSlotsModel:RoomSlotsModel;
		[Inject]
        public var user:UserModel;

        override public function onRegister():void{
            eventMap.mapListener(this.shopMenu, MainMenuEvent.SHOP_MENU_OPEN, this.onShopMenuOpened);
            eventMap.mapListener(this.shopMenu, MainMenuEvent.SHOP_MENU_CLOSED, this.onShopMenuClosed);
            eventMap.mapListener(this.shopMenu, ViewCompEvent.CLICK, this.shopMenuFacadeButton_click);
            eventMap.mapListener(eventDispatcher, BuildingEvent.FACADE_CHANGED, this.toggleFacade);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_START_DRAGGING_CUSTOMIZE_ITEM, this.onStartDraggingCustomizeItem);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_STOP_DRAGGING_CUSTOMIZE_ITEM, this.onStopDraggingCustomizeItem);
            eventMap.mapListener(eventDispatcher, ShopEvent.ON_ITEM_COUNT_CHANGED_BY_PUTTING_BACK, this.onItemChangedByPuttingBack);
            eventMap.mapListener(eventDispatcher, EventConstants.INIT_WORLD_COMPLETE, this.initWorldComplete);
            eventMap.mapListener(eventDispatcher, UserModelEvent.ON_MODEL_CHANGED, this.onUserModelChanged);
            eventMap.mapListener(eventDispatcher, EventConstants.SELECT_SHOP_CATEGORY, this.selectCategory);
            eventMap.mapListener(eventDispatcher, EventConstants.OPEN_SHOP, this.openShop);
            eventMap.mapListener(eventDispatcher, EventConstants.CLOSE_SHOP, this.closeShop);
            this.shopMenu.createFacadeButtons(this.languageModel.getTextByKey("customize_menu_inside_facade_button"), this.languageModel.getTextByKey("customize_menu_outside_facade_button"), this.gameStateModel.isFacadeVisible);
            this.shopMenu.setLabels(this.languageModel.getTextByKey("shop_objects_headline"), this.languageModel.getTextByKey("shop_designs_headline"), this.languageModel.getTextByKey("shop_rooms_headline"), this.languageModel.getTextByKey("shop_floors_headline"), this.languageModel.getTextByKey("shop_walls_headline"));
            this.shopMenu.createHoverLayer();
            this.shopMenu.fitMenuToFacadeState(this.gameStateModel.isFacadeVisible);
            this.shopMenu.tabButtonsRadioGroup.addEventListener(Event.CHANGE, this.tabButtonsRadioGroup_change);
            this.shopMenu.addEventListener(MouseEvent.CLICK, this.shop_click);
            if (this.gameStateModel.initWorldComplete){
                this.initWorldComplete(null);
            };
        }
        private function onUserModelChanged(_arg1:UserModelEvent):void{
            this.updateNumberOfFreeSlots();
        }
        private function shop_click(_arg1:MouseEvent):void{
            dispatch(new Event(EventConstants.HIDE_ARROW_IN_SHOP));
        }
        private function tabButtonsRadioGroup_change(_arg1:Event):void{
            var _local2:String = this.shopMenu.tabButtonsRadioGroup.selectedData();
            dispatch(new InfoEvent(EventConstants.SHOP_TAB_OPEN, _local2));
        }
        private function openShop(_arg1:Event):void{
            if (!this.shopMenu.isOpen()){
                this.shopMenu.show();
            };
        }
        private function closeShop(_arg1:Event):void{
            if (this.shopMenu.isOpen()){
                this.shopMenu.hide();
                this.gameStateModel.isFacadeVisible = false;
            };
        }
        private function initWorldComplete(_arg1:Event):void{
            this.updateNumberOfFreeSlots();
        }
        private function onItemChangedByPuttingBack(_arg1:ShopEvent):void{
            var _local2:ShopItemVO = _arg1.shopItemVO;
            this.shopMenu.selectCategory(_local2.category);
        }
        private function selectCategory(_arg1:InfoEvent):void{
            if (_arg1.info.category){
                this.shopMenu.selectCategory(_arg1.info.category);
            };
        }
        private function shopMenu_click(_arg1:MouseEvent):void{
            dispatch(new Event(EventConstants.PUT_CUSTOMIZE_ITEM_BACK));
            _arg1.stopPropagation();
        }
        private function onStopDraggingCustomizeItem(_arg1:Event):void{
            eventMap.unmapListener(this.shopMenu, MouseEvent.CLICK, this.shopMenu_click);
            this.shopMenu.setNavigationActive(true);
            this.shopMenu.setHoverActive(false);
        }
        private function onStartDraggingCustomizeItem(_arg1:Event):void{
            eventMap.mapListener(this.shopMenu, MouseEvent.CLICK, this.shopMenu_click);
            this.shopMenu.setNavigationActive(false);
            this.shopMenu.setHoverActive(true);
        }
        private function toggleFacade(_arg1:BuildingEvent=null):void{
            this.shopMenu.fitMenuToFacadeState(this.gameStateModel.isFacadeVisible);
        }
        private function shopMenuFacadeButton_click(_arg1:ViewCompEvent):void{
            this.gameStateModel.isFacadeVisible = !(this.gameStateModel.isFacadeVisible);
            var _local2:String = (this.gameStateModel.isFacadeVisible) ? "outside" : "inside";
            var _local3:Object = {type:"shop", subtype1:_local2};
            dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.EVENT, _local3));
        }
        private function onShopMenuClosed(_arg1:Event):void{
            l(("customizeMenuClose: " + this.onShopMenuClosed));
            this.gameStateModel.isFacadeVisible = false;
            this.gameStateModel.isBuildMode = false;
            this.tickModel.pause = false;
            dispatch(new InfoEvent(EventConstants.ON_CLOSE_CUSTOMIZE_MENU, null));
        }
        private function onShopMenuOpened(_arg1:MainMenuEvent):void{
            var _local2:Object = {type:"shop", subtype1:"open"};
            dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.EVENT, _local2));
            this.gameStateModel.isBuildMode = true;
            this.tickModel.pause = true;
            dispatch(new InfoEvent(EventConstants.ON_OPEN_CUSTOMIZE_MENU, null));
            var _local3:String = this.shopMenu.tabButtonsRadioGroup.selectedData();
            dispatch(new InfoEvent(EventConstants.SHOP_TAB_OPEN, _local3));
        }
        private function updateNumberOfFreeSlots():void{
            this.shopMenu.setNumberOfFreeSlots(this.roomSlotsModel.freeSlots);
        }

    }
}//package net.wooga.pets.views.mainmenu 
